myApp.controller('UCProgressController', function ($scope,$http,$stateParams,$interval) {

    $scope.info={};
    $scope.info.tasks=[];
    $scope.info.vmInfo={};
    $scope.info.vmInfo.ip = $stateParams.selectVm;
    $scope.info.vmStatus="";

    $scope.info.zones=[];
    //要展示的zone
    $scope.info.showZone={};
    $scope.info.tabIndex=0;

    $scope.$on("$destroy", function() {
        if($scope.timer){
            $interval.cancel($scope.timer);
        }
    });

    //获取vm信息
    if($scope.info.vmInfo.ip!=''){
        getZones();
    };

    $scope.showTask = function (task) {
        var zone_alias = $scope.info.showZone.zone_alias;
        if(zone_alias == 'xxxxxx'){
            if(task.name.indexOf(':') == -1) {
                return true;
            }else {
                return false;
            }
        }else {
            if(zone_alias == task.name.split(":")[0]){
                return true;
            }else {
                return false;
            }
        }
    };

    $scope.tabClick = function (index,zone) {
        $scope.info.tabIndex = index;
        $scope.info.showZone = zone;
    };

    function getZones() {
        var ip = $scope.info.vmInfo.ip;
        if(typeof ip == 'undefined'
            || ip == 'undefined'
            || ip == ''){
            alert("虚拟机ip为空!");
            return;
        };

        $http.get(getUCloudAddress(ip)+"/zones/",{
            headers : {'Accept':'application/json'}
        }).success(function (data) {
            $scope.info.vmStatus="running";
            $scope.info.zones=data;

            var obj = new Object();
            obj.zone_title = "其他";
            obj.zone_alias = "xxxxxx";
            $scope.info.zones.push(obj);

            $scope.tabClick(0,$scope.info.zones[0]);

            getProgress();
            $scope.timer=$interval(function(){
                getProgress();
            },2000);
        }).error(function () {
            $scope.info.vmStatus="stop";
        });
    };

    function getProgress() {
        var ip = $scope.info.vmInfo.ip;
        if(typeof ip == 'undefined'
            || ip == 'undefined'
            || ip == ''){
            alert("获取进度列表,虚拟机ip为空!");
            return;
        };

        $http.get(getUCloudAddress(ip)+"/pipelines/",{
            headers : {'Accept':'application/json'}
        }).success(function (data) {
            $scope.info.vmStatus="running";
            $scope.info.tasks=[];
            angular.forEach(data,function (task) {
                console.log(JSON.stringify(task));
                if(task.status == 'READY' || task.status == 'DONE'){
                    task.progressType="success";
                }else if(task.status == 'RUNNING' || task.status == 'PENDING'){
                    task.progressType="info";
                }else if(task.status == 'UNAVAIABLE' || task.status == 'FAILED'){
                    task.progressType="danger";
                }else {
                    task.progressType="info";
                }
                if(task.status != 'READY'){
                    $scope.info.tasks.push(task);
                }
            });
        }).error(function () {
            $scope.info.vmStatus="stop";
        });
    };
});